<template>
  <div class="app-container">

    <el-button @click="drawer = true" type="primary" style="margin-left: 16px;">
      微信导航栏配置
    </el-button>

    <el-drawer
      size="50%"
      :visible.sync="drawer"
      :direction="direction">
      <tabbar/>
    </el-drawer>

    <!-- 添加或修改微信支付对话框 -->
    <el-form label-position="top" ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="AppID(小程序ID)" prop="appId">
        <el-input v-model="form.appId" placeholder="请输入应用编号"/>
      </el-form-item>
      <el-form-item label="AppSecret(小程序密钥)" prop="apiKey">
        <el-input v-model="form.appSecret" placeholder="请输入秘钥"/>
      </el-form-item>
      <el-form-item label="用户协议" prop="userAgreement">
        <editor v-model="form.userAgreement" :min-height="192"/>
      </el-form-item>
      <el-form-item label="隐私协议" prop="privacyAgreement">
        <editor v-model="form.privacyAgreement" :min-height="192"/>
      </el-form-item>
    </el-form>
    <div style="display: flex;justify-content: center" slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </div>
</template>

<script>

import {addWechatConfig, allWechatConfigs, updateWechatConfig} from "@/api/system/wechat_config";

import tabbar from "@/views/system/tabbar/index.vue";


export default {
  components: {tabbar},
  data() {
    return {
      drawer: false,
      direction: 'rtl',
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getInfo();
  },
  methods: {
    getInfo() {
      allWechatConfigs().then(re => {
        if (re.data.length > 0) {
          this.form = re.data[0]
        } else {
          this.form = {}
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWechatConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getInfo();
            });
          } else {
            addWechatConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getInfo();
            });
          }
        }
      });
    },
  }
};
</script>
